package scene;

import geometrie.Point;
import geometrie.Rebouchage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Class implementing a Cube as a set of Facette3D objects.
 * 
 */
public class Cube implements Modele3D {
	private List<Facette3D> facettes;

	/**
	 * Return to get the a iterator over the facette3D
	 * 
	 * @return the iterator
	 */
	public Iterator<Facette3D> getFacettes() {
		return facettes.listIterator();
	}

	/**
	 * The constructor of cube
	 * 
	 * @param size
	 *            The cube's edges size
	 * @param start
	 *            The point which the bottom left corner will be placed
	 */
	public Cube(double size, Point start) {
		Point[] vertexArray = new Point[8];
		facettes = new ArrayList<Facette3D>(12);
		double i, j, k;
		int counter = 0;

		for (k = start.getZCartesien(); k >= (start.getZCartesien() - size); k -= size) {
			for (j = start.getYCartesien(); j <= start.getYCartesien() + size; j += size) {
				for (i = start.getXCartesien(); i <= start.getXCartesien()
						+ size; i += size) {
					vertexArray[counter] = new Point(i, j, k);
					counter++;
				}
			}
		}
		facettes.add(new Facette3D(vertexArray[0], vertexArray[1],
				vertexArray[2]));
		facettes.add(new Facette3D(vertexArray[1], vertexArray[2],
				vertexArray[3]));
		facettes.add(new Facette3D(vertexArray[4], vertexArray[5],
				vertexArray[6]));
		facettes.add(new Facette3D(vertexArray[5], vertexArray[6],
				vertexArray[7]));
		facettes.add(new Facette3D(vertexArray[0], vertexArray[1],
				vertexArray[4]));
		facettes.add(new Facette3D(vertexArray[1], vertexArray[4],
				vertexArray[5]));
		facettes.add(new Facette3D(vertexArray[2], vertexArray[3],
				vertexArray[6]));
		facettes.add(new Facette3D(vertexArray[3], vertexArray[6],
				vertexArray[7]));
		facettes.add(new Facette3D(vertexArray[0], vertexArray[2],
				vertexArray[4]));
		facettes.add(new Facette3D(vertexArray[2], vertexArray[4],
				vertexArray[6]));
		facettes.add(new Facette3D(vertexArray[1], vertexArray[3],
				vertexArray[5]));
		facettes.add(new Facette3D(vertexArray[3], vertexArray[5],
				vertexArray[7]));
	}

	@Override
	public String toString() {
		return "Cube [facettes=" + facettes + "]";
	}

	@Override
	public void addTriangulation(Point[] polygon) {
		facettes.addAll(Rebouchage.rebouchageTrouRecCache(polygon));
	}


	/*
	 * Main defined by unity testing proposes public static void main(String []
	 * args) { Modele3D cube = new Cube(10, new Point(0,0,0));
	 * System.out.println(cube); cube = new Cube(20, new Point(1,1,1));
	 * System.out.println(cube); }
	 */
}
